#include "CDxDevice.h"

#pragma region Constructors & Destructor

//////////////////////////////////////////////////////////////////////////
CDxDevice::CDxDevice(void)
{

}

//////////////////////////////////////////////////////////////////////////
CDxDevice::~CDxDevice(void)
{
}

//////////////////////////////////////////////////////////////////////////
CDxDevice::CDxDevice(HWND hWnd)
{
	Create_Direct9();
	Create_D3DPP(hWnd);
	CreateDXDevice(hWnd);
}

#pragma endregion 

#pragma region Methods

//////////////////////////////////////////////////////////////////////////
bool CDxDevice::Create_Direct9()
{
	m_d3d = Direct3DCreate9(D3D_SDK_VERSION);
	if(m_d3d == NULL)
	{
		MessageBox(NULL,"Create D3D - Failed","Error",MB_OK | MB_ICONERROR);
		return false;
	}
	return true;
}

//////////////////////////////////////////////////////////////////////////
bool CDxDevice::Create_D3DPP(HWND hWnd)
{
	ZeroMemory(&m_d3dpp,sizeof(m_d3dpp));

	m_d3dpp.BackBufferCount = 1;
	m_d3dpp.Windowed = true;
	m_d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
	m_d3dpp.BackBufferWidth = 800;
	m_d3dpp.BackBufferHeight = 600;
	m_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;

	return true;
}

//////////////////////////////////////////////////////////////////////////
bool CDxDevice::CreateDXDevice(HWND hWnd)
{
	m_d3d->CreateDevice(
		D3DADAPTER_DEFAULT,
		D3DDEVTYPE_REF,
		hWnd,
		D3DCREATE_SOFTWARE_VERTEXPROCESSING,
		&m_d3dpp,
		&m_d3ddev);
	if(m_d3ddev == NULL)
	{
		MessageBox(NULL,"Create D3DDevice - Failed","ERROR",MB_OK | MB_ICONERROR);
		return false;
	}
	return true;
}

//////////////////////////////////////////////////////////////////////////
void CDxDevice::LoadImageFromFile(char* filePath,D3DCOLOR transColor)
{
	HRESULT result;
	D3DXIMAGE_INFO info;
	result = D3DXGetImageInfoFromFile(filePath,&info);
	if(result != D3D_OK)
	{
		return;
	}
	result = m_d3ddev->GetBackBuffer(
		0,
		0,
		D3DBACKBUFFER_TYPE_MONO,
		&m_backbuffer);
	m_d3ddev->CreateOffscreenPlainSurface(
		info.Width,
		info.Height,
		D3DFMT_X8R8G8B8,
		D3DPOOL_DEFAULT,
		&m_surface,
		NULL);
	if(result != D3D_OK)
	{
		return;
	}
	result = D3DXLoadSurfaceFromFile(
		m_surface,
		NULL,
		NULL,
		filePath,
		NULL,
		D3DX_DEFAULT,
		transColor,
		NULL);
	if(result != D3D_OK)
	{
		return;
	}
}

//////////////////////////////////////////////////////////////////////////
LPDIRECT3DDEVICE9 CDxDevice::GetDevice()
{
	return m_d3ddev;
}

//////////////////////////////////////////////////////////////////////////
LPDIRECT3D9 CDxDevice::GetD3D()
{
	return m_d3d;
}

//////////////////////////////////////////////////////////////////////////
D3DPRESENT_PARAMETERS CDxDevice::GetD3DPP()
{
	return m_d3dpp;
}

//////////////////////////////////////////////////////////////////////////
LPDIRECT3DSURFACE9 CDxDevice::GetSurface()
{
	return m_surface;
}

//////////////////////////////////////////////////////////////////////////
LPDIRECT3DSURFACE9 CDxDevice::GetBackbuffer()
{
	return m_backbuffer;
}

#pragma endregion 